function [grid_data] = grid_model()
% 智能电网模型定义
% 包含100个节点的测试电网，用于验证SAEA-RFS算法

% 电网基本参数
grid_data.num_nodes = 100;           % 节点数量
grid_data.num_generators = 30;       % 发电机数量
grid_data.num_loads = 70;            % 负荷数量
grid_data.num_lines = 120;           % 线路数量
grid_data.num_storage = 10;          % 储能设备数量
grid_data.time_periods = 24;         % 调度时段数（24小时）

% 发电机参数
grid_data.generators = struct();
for i = 1:grid_data.num_generators
    grid_data.generators(i).node = i;                    % 所在节点
    grid_data.generators(i).max_power = 50 + 50*rand();  % 最大出力(MW)
    grid_data.generators(i).min_power = 5 + 5*rand();    % 最小出力(MW)
    grid_data.generators(i).ramp_up = 10 + 5*rand();     % 爬坡上限(MW/h)
    grid_data.generators(i).ramp_down = 10 + 5*rand();   % 爬坡下限(MW/h)
    grid_data.generators(i).cost_a = 50 + 100*rand();    % 成本系数a
    grid_data.generators(i).cost_b = 10 + 20*rand();     % 成本系数b
    grid_data.generators(i).cost_c = 0.1 + 0.2*rand();   % 成本系数c
    grid_data.generators(i).carbon_factor = 0.5 + 0.5*rand(); % 碳排放因子
end

% 负荷参数
grid_data.loads = struct();
for i = 1:grid_data.num_loads
    grid_data.loads(i).node = grid_data.num_generators + i;  % 所在节点
    grid_data.loads(i).base_load = 20 + 30*rand();          % 基础负荷(MW)
    grid_data.loads(i).load_profile = generate_load_profile(); % 24小时负荷曲线
end

% 储能设备参数
grid_data.storage = struct();
for i = 1:grid_data.num_storage
    grid_data.storage(i).node = 80 + i;                     % 所在节点
    grid_data.storage(i).capacity = 20 + 30*rand();         % 容量(MWh)
    grid_data.storage(i).max_power = 5 + 10*rand();         % 最大功率(MW)
    grid_data.storage(i).efficiency = 0.85 + 0.1*rand();    % 充放电效率
    grid_data.storage(i).initial_soc = 0.5 + 0.3*rand();    % 初始荷电状态
end

% 线路参数
grid_data.lines = struct();
for i = 1:grid_data.num_lines
    grid_data.lines(i).from_node = randi(grid_data.num_nodes);
    grid_data.lines(i).to_node = randi(grid_data.num_nodes);
    while grid_data.lines(i).to_node == grid_data.lines(i).from_node
        grid_data.lines(i).to_node = randi(grid_data.num_nodes);
    end
    grid_data.lines(i).resistance = 0.01 + 0.05*rand();     % 电阻(p.u.)
    grid_data.lines(i).reactance = 0.05 + 0.15*rand();      % 电抗(p.u.)
    grid_data.lines(i).capacity = 50 + 100*rand();          % 容量(MW)
end

% 新能源出力预测（简化模型）
grid_data.renewable = struct();
grid_data.renewable.solar_capacity = 200;  % 光伏装机容量(MW)
grid_data.renewable.wind_capacity = 300;   % 风电装机容量(MW)
grid_data.renewable.solar_profile = generate_solar_profile(); % 光伏出力曲线
grid_data.renewable.wind_profile = generate_wind_profile();   % 风电出力曲线

fprintf('电网模型初始化完成：\n');
fprintf('- 节点数量: %d\n', grid_data.num_nodes);
fprintf('- 发电机数量: %d\n', grid_data.num_generators);
fprintf('- 负荷数量: %d\n', grid_data.num_loads);
fprintf('- 储能设备数量: %d\n', grid_data.num_storage);
fprintf('- 线路数量: %d\n', grid_data.num_lines);
end

function profile = generate_load_profile()
% 生成24小时负荷曲线（简化模型）
base_hours = [1:6, 22:24];  % 低谷时段
peak_hours = [8:12, 18:21]; % 高峰时段
normal_hours = [7, 13:17];  % 平峰时段

profile = ones(1, 24);
profile(base_hours) = 0.6 + 0.2*rand(1, length(base_hours));   % 低谷负荷
profile(peak_hours) = 1.2 + 0.3*rand(1, length(peak_hours));   % 高峰负荷
profile(normal_hours) = 0.9 + 0.2*rand(1, length(normal_hours)); % 平峰负荷
end

function profile = generate_solar_profile()
% 生成24小时光伏出力曲线
profile = zeros(1, 24);
day_hours = 6:18;  % 白天时段
peak_hour = 12;    % 正午时段

for hour = day_hours
    if hour <= peak_hour
        profile(hour) = 0.3 + 0.7 * (hour - 6) / (peak_hour - 6);
    else
        profile(hour) = 0.3 + 0.7 * (18 - hour) / (18 - peak_hour);
    end
    profile(hour) = profile(hour) * (0.8 + 0.4*rand()); % 添加随机波动
end
end

function profile = generate_wind_profile()
% 生成24小时风电出力曲线
profile = zeros(1, 24);
for hour = 1:24
    % 风电出力具有随机性，但有一定的时间相关性
    if hour == 1
        profile(hour) = 0.3 + 0.7*rand();
    else
        % 与前一小时相关
        profile(hour) = 0.7*profile(hour-1) + 0.3*(0.3 + 0.7*rand());
    end
    profile(hour) = max(0, min(1, profile(hour))); % 限制在[0,1]范围内
end
end

